package com.dianping.cat.consumer.storage.model.transform;

import com.dianping.cat.consumer.storage.model.IEntity;
import com.dianping.cat.consumer.storage.model.entity.Domain;
import com.dianping.cat.consumer.storage.model.entity.Machine;
import com.dianping.cat.consumer.storage.model.entity.Operation;
import com.dianping.cat.consumer.storage.model.entity.Segment;
import com.dianping.cat.consumer.storage.model.entity.Sql;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;













public class DefaultSaxParser
  extends DefaultHandler
{
/*  43 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  45 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  47 */   private Stack<String> m_tags = new Stack();

/*  49 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  53 */   private StringBuilder m_text = new StringBuilder();


/*  56 */   public static StorageReport parse(InputStream in) throws SAXException, IOException { return (StorageReport)parseEntity(StorageReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  60 */   public static StorageReport parse(Reader reader) throws SAXException, IOException { return (StorageReport)parseEntity(StorageReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  64 */   public static StorageReport parse(String xml) throws SAXException, IOException { return (StorageReport)parseEntity(StorageReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  70 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  71 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  73 */       factory.setValidating(false);
/*  74 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  76 */       factory.newSAXParser().parse(is, handler);
/*  77 */       return (T)handler.getEntity();
    } catch (ParserConfigurationException e) {
/*  79 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  84 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  88 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  94 */     if (obj instanceof String) {
/*  95 */       String str = (String)obj;

/*  97 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  98 */         return (T)str.substring(1);
      }
/* 100 */       return obj;
    }
/* 102 */     if (obj instanceof InputStream) {
/* 103 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 105 */       in.mark(3);

/* 107 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 108 */         in.reset();
      }

/* 111 */       return (T)in;
/* 112 */     }  if (obj instanceof Reader) {
/* 113 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 115 */       in.mark(1);

/* 117 */       if (in.read() != 65279) {
/* 118 */         in.reset();
      }

/* 121 */       return (T)in;
    }
/* 123 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 129 */     if (value == null || value.length() == 0) {
/* 130 */       return defaultValue;
    }

/* 133 */     if (type == Boolean.class)
/* 134 */       return (T)Boolean.valueOf(value); 
/* 135 */     if (type == Integer.class)
/* 136 */       return (T)Integer.valueOf(value); 
/* 137 */     if (type == Long.class)
/* 138 */       return (T)Long.valueOf(value); 
/* 139 */     if (type == Short.class)
/* 140 */       return (T)Short.valueOf(value); 
/* 141 */     if (type == Float.class)
/* 142 */       return (T)Float.valueOf(value); 
/* 143 */     if (type == Double.class)
/* 144 */       return (T)Double.valueOf(value); 
/* 145 */     if (type == Byte.class)
/* 146 */       return (T)Byte.valueOf(value); 
/* 147 */     if (type == Character.class) {
/* 148 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 150 */     return (T)value;
  }




/* 156 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 161 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 166 */     if (uri == null || uri.length() == 0) {
/* 167 */       Object currentObj = this.m_objs.pop();
/* 168 */       String currentTag = (String)this.m_tags.pop();

/* 170 */       if (currentObj instanceof StorageReport) {
/* 171 */         StorageReport storageReport = (StorageReport)currentObj;

/* 173 */         if ("id".equals(currentTag)) {
/* 174 */           storageReport.addId(getText());
/* 175 */         } else if ("ip".equals(currentTag)) {
/* 176 */           storageReport.addIp(getText());
/* 177 */         } else if ("op".equals(currentTag)) {
/* 178 */           storageReport.addOp(getText());
        }
      }
    }

/* 183 */     this.m_text.setLength(0);
  }


/* 187 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 191 */   protected String getText() { return this.m_text.toString(); }


  private void parseForDomain(Domain parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 195 */     if ("operation".equals(qName)) {
/* 196 */       Operation operation = this.m_maker.buildOperation(attributes);

/* 198 */       this.m_linker.onOperation(parentObj, operation);
/* 199 */       this.m_objs.push(operation);
/* 200 */     } else if ("sql".equals(qName)) {
/* 201 */       Sql sql = this.m_maker.buildSql(attributes);

/* 203 */       this.m_linker.onSql(parentObj, sql);
/* 204 */       this.m_objs.push(sql);
    } else {
/* 206 */       throw new SAXException(String.format("Element(%s) is not expected under domain!", new Object[] { qName }));
    }

/* 209 */     this.m_tags.push(qName);
  }

  private void parseForMachine(Machine parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 213 */     if ("domain".equals(qName)) {
/* 214 */       Domain domain = this.m_maker.buildDomain(attributes);

/* 216 */       this.m_linker.onDomain(parentObj, domain);
/* 217 */       this.m_objs.push(domain);
    } else {
/* 219 */       throw new SAXException(String.format("Element(%s) is not expected under machine!", new Object[] { qName }));
    }

/* 222 */     this.m_tags.push(qName);
  }

  private void parseForOperation(Operation parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 226 */     if ("segment".equals(qName)) {
/* 227 */       Segment segment = this.m_maker.buildSegment(attributes);

/* 229 */       this.m_linker.onSegment(parentObj, segment);
/* 230 */       this.m_objs.push(segment);
    } else {
/* 232 */       throw new SAXException(String.format("Element(%s) is not expected under operation!", new Object[] { qName }));
    }

/* 235 */     this.m_tags.push(qName);
  }

  private void parseForSegment(Segment parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 239 */     this.m_objs.push(parentObj);
/* 240 */     this.m_tags.push(qName);
  }

  private void parseForSql(Sql parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 244 */     this.m_objs.push(parentObj);
/* 245 */     this.m_tags.push(qName);
  }

  private void parseForStorageReport(StorageReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 249 */     if ("ids".equals(qName) || "id".equals(qName) || "ips".equals(qName) || "ip".equals(qName) || "ops".equals(qName) || "op".equals(qName)) {
/* 250 */       this.m_objs.push(parentObj);
/* 251 */     } else if ("machine".equals(qName)) {
/* 252 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 254 */       this.m_linker.onMachine(parentObj, machine);
/* 255 */       this.m_objs.push(machine);
    } else {
/* 257 */       throw new SAXException(String.format("Element(%s) is not expected under storage-report!", new Object[] { qName }));
    }

/* 260 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 264 */     if ("storage-report".equals(qName)) {
/* 265 */       StorageReport storageReport = this.m_maker.buildStorageReport(attributes);

/* 267 */       this.m_entity = storageReport;
/* 268 */       this.m_objs.push(storageReport);
/* 269 */       this.m_tags.push(qName);
/* 270 */     } else if ("machine".equals(qName)) {
/* 271 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 273 */       this.m_entity = machine;
/* 274 */       this.m_objs.push(machine);
/* 275 */       this.m_tags.push(qName);
/* 276 */     } else if ("domain".equals(qName)) {
/* 277 */       Domain domain = this.m_maker.buildDomain(attributes);

/* 279 */       this.m_entity = domain;
/* 280 */       this.m_objs.push(domain);
/* 281 */       this.m_tags.push(qName);
/* 282 */     } else if ("operation".equals(qName)) {
/* 283 */       Operation operation = this.m_maker.buildOperation(attributes);

/* 285 */       this.m_entity = operation;
/* 286 */       this.m_objs.push(operation);
/* 287 */       this.m_tags.push(qName);
/* 288 */     } else if ("segment".equals(qName)) {
/* 289 */       Segment segment = this.m_maker.buildSegment(attributes);

/* 291 */       this.m_entity = segment;
/* 292 */       this.m_objs.push(segment);
/* 293 */       this.m_tags.push(qName);
/* 294 */     } else if ("sql".equals(qName)) {
/* 295 */       Sql sql = this.m_maker.buildSql(attributes);

/* 297 */       this.m_entity = sql;
/* 298 */       this.m_objs.push(sql);
/* 299 */       this.m_tags.push(qName);
    } else {
/* 301 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 307 */     if (uri == null || uri.length() == 0) {
/* 308 */       if (this.m_objs.isEmpty()) {
/* 309 */         parseRoot(qName, attributes);
      } else {
/* 311 */         Object parent = this.m_objs.peek();
/* 312 */         String tag = (String)this.m_tags.peek();

/* 314 */         if (parent instanceof StorageReport) {
/* 315 */           parseForStorageReport((StorageReport)parent, tag, qName, attributes);
/* 316 */         } else if (parent instanceof Machine) {
/* 317 */           parseForMachine((Machine)parent, tag, qName, attributes);
/* 318 */         } else if (parent instanceof Domain) {
/* 319 */           parseForDomain((Domain)parent, tag, qName, attributes);
/* 320 */         } else if (parent instanceof Operation) {
/* 321 */           parseForOperation((Operation)parent, tag, qName, attributes);
/* 322 */         } else if (parent instanceof Segment) {
/* 323 */           parseForSegment((Segment)parent, tag, qName, attributes);
/* 324 */         } else if (parent instanceof Sql) {
/* 325 */           parseForSql((Sql)parent, tag, qName, attributes);
        } else {
/* 327 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 331 */       this.m_text.setLength(0);
    } else {
/* 333 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 339 */       return (new SimpleDateFormat(format)).parse(str);
/* 340 */     } catch (ParseException e) {
/* 341 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format) {
    try {
/* 347 */       return (new DecimalFormat(format)).parse(str);
/* 348 */     } catch (ParseException e) {
/* 349 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/storage/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */